package com.bawei.common_module.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

/**
 * 作者: 李韵佳
 * 日期: 2024/8/15
 * 时间: 14:50
 * 描述:
 */
abstract class BaseActivity<VM: ViewModel,D: ViewDataBinding>(private val layoutId:Int) :
    AppCompatActivity(){
    lateinit var viewModel: VM
    lateinit var binding:D
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= DataBindingUtil.setContentView(this,layoutId)

        val type = javaClass.genericSuperclass as ParameterizedType
        val vmClass = type.actualTypeArguments[0] as Class<VM>
        viewModel= ViewModelProvider(this)[vmClass]

        initViews()

        dataObserver()
    }

    abstract fun dataObserver()

    abstract fun initViews()

    override fun onDestroy() {
        super.onDestroy()
        if(binding!=null){
            binding.unbind()
        }
    }
}